/**
 * localStorage管理
 */
export class LocalStorageManager {
    /**获取JSON */
    getJson(key: string) {
        const value = window.localStorage.getItem(key) || "null";
        return JSON.parse(value);
    }

    /**获取字符串 */
    getStr(key: string) {
        return window.localStorage.getItem(key);
    }

    /** 设置值 */
    set(key: string, value: any) {
        /**数组、对象都转字符串 */
        const isObj = typeof value === 'object';
        const strVal = isObj ? JSON.stringify(value) : value;
        window.localStorage.setItem(key, strVal);
    }
}

/** 将我的好友ID、群聊ID 保存至 localStorage */
export function saveMyFriendsToLocalStorage(data: any) {
    data = JSON.stringify(data);
    window.localStorage.setItem('my-friends', data);
}
export function saveMyGroupToLocalStorage(data: any) {
    data = JSON.stringify(data);
    window.localStorage.setItem('my-groups', data);
}

/**
 * 保存最近的好友id
 * @param friendId 好友id
 */
export function saveRecentFriendId(friendId: string) {
    const storage = window.localStorage;
    let recentStr = (storage.getItem('recent-friend') || '');
    const recentArr = recentStr.split(',');
    if (recentArr.includes(friendId)) return;
    recentStr = recentStr ? recentStr + ',' + friendId : friendId;
    // console.log("执行时机----保存本地好友的id：", recentStr);
    storage.setItem('recent-friend', recentStr);
}

/**
 * 保存最近的群id
 * @param groupId 群id
 */
export function saveRecentGroupId(groupId: string) {
    const storage = window.localStorage;
    let recentStr = (storage.getItem('recent-group') || '');
    const recentArr = recentStr.split(',');
    if (recentArr.includes(groupId)) return;
    recentStr = recentStr ? recentStr + ',' + groupId : groupId;
    storage.setItem('recent-group', recentStr);
}

/**
 * 获取最近的好友ids
 */
export function getRecentFriendIds() {
    const storage = window.localStorage;
    return (storage.getItem('recent-friend') || '')
}

/**
 * 获取最近的群聊ids
 */
export function getRecentGroupIds() {
    const storage = window.localStorage;
    return (storage.getItem('recent-group') || '')
}

/**
 * 删除最近的好友id
 */
export function delRecentFriendId() {
    const storage = window.localStorage;
    storage.removeItem('recent-friend');
}

/**
 * 删除最近的群聊id
 */
export function delRecentGroupId() {
    const storage = window.localStorage;
    storage.removeItem('recent-friend');
}

